package com.running.base.run.interceptor;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;


public class AuthenticationInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(AuthenticationInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        logger.info("AuthenticationInterceptor preHandle begin...");
        Enumeration<String> headers = httpServletRequest.getHeaderNames();
        logger.info("Http header is={}", JSON.toJSONString(headers));
        String requestMethod = httpServletRequest.getMethod();
        String requestUrl = httpServletRequest.getServletPath();

        logger.info("Http request method=" +requestMethod+", path_url=" + requestUrl);

        logger.info("AuthenticationInterceptor preHandle end...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object, ModelAndView modelAndView) throws Exception {
        //TODO nothing
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object, Exception e) throws Exception {
        //TODO nothing
    }
}
